package com.xaicode.learn.util.timer;

import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;

import java.util.TimerTask;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * count ThreadPool thread number
 *
 * @author Locker xaicode@sina.com
 * @version 1.0
 * @date 2021/6/12 0012 23:34
 */
@Slf4j
@EqualsAndHashCode(callSuper = true)
public class ThreadPoolTimer extends TimerTask {

    ThreadPoolExecutor threadPoolExecutor;

    Type type;

    @Override
    public void run() {
        switch (type) {
            case CORE_POOL_SIZE:
                log.info("current thread pool [{}] core pool size is [{}]", Thread.currentThread().getId(), threadPoolExecutor.getCorePoolSize());
                break;
            case NORMAL:
                break;
            case ACTIVE_COUNT:
            default:
                log.info("current thread pool [{}] has [{}] active thread", Thread.currentThread().getId(), threadPoolExecutor.getActiveCount());
                break;
        }
    }

    public ThreadPoolTimer() {
    }

    public ThreadPoolTimer(ThreadPoolExecutor threadPoolExecutor) {
        this.threadPoolExecutor = threadPoolExecutor;
    }

    public ThreadPoolTimer(ThreadPoolExecutor threadPoolExecutor, Type type) {
        this.threadPoolExecutor = threadPoolExecutor;
        this.type = type;
    }

    // ----

    public enum Type {
        ACTIVE_COUNT, // with default
        CORE_POOL_SIZE,
        NORMAL;
    }
}


